home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16067 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: news.compuserve.com!newsmaster
  2. From: 76623,2065@compuserve.com  (Bobby Martin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Operator Overloading
  5. Date: 9 Apr 1996 13:01:47 GMT
  6. Organization: CompuServe Incorporated
  7. Message-ID: <4kdn3r$rb9@arl-news-svc-2.compuserve.com>
  8. References: <9604071905.AA001o6@lorelei.demon.co.uk>
  9. Reply-To: 76623,2065@compuserve.com (Bobby Martin)
  10. NNTP-Posting-Host: hd23-013.compuserve.com
  11. X-Newsreader: IBM NewsReader/2 v1.03
  12.  
  13. You have in fact found user-defined casting operators.  They allow you to
  14. define ways in which your class can be *automatically* converted into the
  15. type you specify.  The definition in the streams library allows the stream to
  16. be automatically converted to a void pointer, and the one you defined in
  17. your class allows it to be automatically converted into a double.
  18.  
  19. Note that these functions will be called whenever the compiler determines that
  20. the type casted to is required and the class in which the cast is defined is
  21. given.  There is no warning that this function is called.  For example, if you
  22. define a String class that has an operator const char*() const defined, the
  23. following will compile:
  24.  
  25. String name("Bobby Martin");
  26.  
  27. strcmp( name, "John Hancock");
  28.  
  29. This can be very useful in some cases, but can lead to code that's confusing
  30. to the user on second glance.
  31.  
  32. Hope that helps,
  33.  
  34. Bobby Martin
  35.  
  36. In <9604071905.AA001o6@lorelei.demon.co.uk>, John Croudy <john@lorelei.demon.co.uk> writes:
  37. >Hello,
  38. >
  39. >I have a question which I can't find the answer to in any of my books.
  40. ..
  41. <snip>
  42. ..
  43. >       operator void*() const { return something; }
  44. >
  45. ..
  46. <snip>
  47. ..
  48. >It seems to be a user-defined casting operator, but I can't find any
  49. >mention of this in any books I have.  Can anyone explain what I have
  50. >discovered here?
  51. >
  52. >Thanks,
  53. >
  54. >John
  55. >xxxx
  56.  
  57.